Get the First Row of Pandas using iloc[]
This method is used to access the row by using row numbers. We can get the first row by using 0 indexes.
Example 1: Python code to get the first row of the Dataframe by using the iloc[] function
Python3
# import pandas module import pandas as pd # create dataframe with 3 columns data = pd.DataFrame({ "id" : [ 7058 , 7059 , 7072 , 7054 ], "name" : [ 'sravan' , 'jyothika' , 'harsha' , 'ramya' ], "subjects" : [ 'java' , 'python' , 'html/php' , 'php/js' ] } ) # get first row using row position print (data.iloc[ 0 ]) print ( "---------------" ) # get first row using slice operator print (data.iloc[: 1 ]) |
Output:
id 7058 name sravan subjects java Name: 0, dtype: object --------------- id name subjects 0 7058 sravan java
Example 2: Get the first row for a particular column
Python3
# import pandas module import pandas as pd # get first row using row position print (data[ 'name' ].iloc[ 0 ]) print ( "---------------" ) # get first row using slice operator print (data[ 'subjects' ].iloc[: 1 ]) |
Output:
sravan --------------- 0 java Name: subjects, dtype: object
How to Get First Row of Pandas DataFrame?
In this article, we will discuss how to get the first row of the Pandas Dataframe